package ru.tulupov.quadraticequation;

import java.util.List;

public class Main {

	public static void main(String[] args) {
		QuadraticEquationParser parser = new QuadraticEquationParser(System.in);
		QuadraticEquation equation = null;

		System.out.println("Quadratic equation solver.");
		System.out
				.println("Please enter coefficients for equation Ax^2 + Bx + C = 0 respectively.");

		while (equation == null) {
			try {
				equation = parser.parse();
			} catch (ParseException e) {
				System.err.println("Wrong input!");
			} catch (EOFParseException e) {
				System.exit(0);
			} catch (InvalidQuadraticEquation e) {
				System.err.println("Invalid quadratic equation!");
			}
		}

		QuadraticEquationSolver solver = new QuadraticEquationSolver(equation);
		List<Double> roots = solver.getRoots();

		if (roots.size() == 0) {
			System.out.println("The equation hasn't rational roots.");
		} else if (roots.size() == 1) {
			System.out.println("The equation has one root.");
			System.out.println(String.format("x = %f", roots.get(0)));
		} else if (roots.size() == 2) {
			System.out.println("The equation has two roots.");
			System.out.println(String.format("x1 = %f", roots.get(0)));
			System.out.println(String.format("x2 = %f", roots.get(1)));
		}
	}
}
